<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :before-upload="beforeUpload"
      :http-request="upload"
      :on-preview="preview"
      :on-remove="handleRemove"
      :on-change="changeFile"
      :file-list.sync="list"
      :class="{ disabled: fileComputed}"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showPercent" :percentage="percent" style="width: 180px" />
    <el-dialog title="图片预览" :visible.sync="showDialog">
      <img v-imagerror="require('@/assets/common/bigUserHeader.png')" :src="imgUrl" style="width: 100%" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID9B1kdI6Yu1va2Hk2Z5OwHNDYiett6V5R',
  SecretKey: 'HIL8fDWNxGTSdbM4g27TaPxEFyQaU84J'
})
export default {
  data() {
    return {
      list: [/* { url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201412%2F29%2F20141229151653_HiM8B.thumb.700_0.jpeg&refer=http%3A%2F%2Fcdn.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621649058&t=3b6049f49f17854ee09e4724adbb5352' }*/],
      showDialog: false,
      imgUrl: '',
      currentFileUid: null,
      percent: 0,
      showPercent: false
    }
  },
  computed: {
    fileComputed() {
      return this.list.length === 1
    }
  },
  methods: {
    preview(file) {
      this.imgUrl = file.url
      this.showDialog = true
    },
    handleRemove(file, fileList) {
      // this.list = fileList
      this.list = this.list.filter(item => item.uid !== file.uid)
    },
    changeFile(file, fileList) {
      if (!this.list.some(item => item.uid === file.uid)) {
        this.list.push(file)
      }
      // console.log(fileList.length)
      // this.list = fileList.map(item => item)
      // console.log(this.list)
    },
    beforeUpload(file) {
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!')
        return false
      }
      //  检查大小
      const maxSize = 5 * 1024 * 1024
      if (maxSize < file.size) {
        this.$message.error('图片大小最大不能超过5M')
        return false
      }
      this.currentFileUid = file.uid
      this.showPercent = true
      return true
    },
    upload(params) {
      if (params.file) {
        // 执行上传操作
        cos.putObject({
          Bucket: 'yishuihan-101-1305686580', // 存储桶
          Region: 'ap-nanjing', // 地域
          Key: params.file.name, // 文件名
          Body: params.file, // 要上传的文件对象
          StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
          // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件名  文件对象  格式 回调
          onProgress: (params) => {
            console.log(params.percent)
            this.percent = params.percent * 100
          }
        }, (err, data) => {
          // data返回数据之后 应该如何处理
          // console.log(err || data)
          if (!err && data.statusCode === 200) {
            this.list = this.list.map(item => {
              if (item.uid === this.currentFileUid) {
                return { url: `http://${data.Location}`, upload: true }
              }
              return item
            })

            // console.log(this.list)
          }
        })
        setTimeout(() => {
          this.showPercent = false
          this.percent = 0
        }, 1000)
      }
    }
  }

}
</script>

<style>
.disabled .el-upload--picture-card {
     display: none;
}
</style>
